﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity.Model;
using SystemFrameWork.Enums;
using SystemFrameWork;

namespace DutyWeb.Admin
{
    public partial class DepartmentManagement : BasePage
    {
        /// <summary>
        /// edit flag of this page
        /// </summary>
        private bool editFlag
        {
            get
            {
                if (this.LabEditFlag.Text == ConstantsManager.True)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            set
            {
                this.LabEditFlag.Text = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void initialize()
        {
            string id = Request.QueryString["id"];
            if (StringHelper.IsGUID(id))
            {
                this.editFlag = true;


            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SaveDepartment();
        }

        private void SaveDepartment()
        {
            Department d = new Department();
            d.ID = Guid.NewGuid();
            d.DepartmentAlias = this.txtDepartmentAlias.Text.Trim();
            d.Description = this.txtDepartmentComment.Text.Trim();
            d.DepartmentName = this.txtDepartmentName.Text.Trim();
            d.CreatedOn = DateTime.Now;
            d.ModifiedOn = DateTime.Now;
            d.ParentID = this.DepartmentTree1.SelectedDepartmentID;
            d.ParentDNA = string.Format("{0}{1}", this.DepartmentTree1.SelectedParentDNA, Guid.NewGuid().ToString().Replace("-", ""));

            if (d.ParentID == null)
            {
                base.ShowMessage("上级部门不能为空!", ExceptionLevel.Warn);
                return;
            }

            Department dx = base.departmentManagement.GetDepartmentByName(d);
            if (dx != null)
            {
                base.ShowMessage("同一个单位中不能有重名的部门!", ExceptionLevel.Warn);
                return;
            }

            base.departmentManagement.AddDepartment(d);
            base.departmentManagement.UpdateChanges();
            this.btnCancel_Click(null, null);
            base.ShowMessage("保存成功!", ExceptionLevel.Info);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtDepartmentAlias.Text = string.Empty;
            this.txtDepartmentComment.Text = string.Empty;
            this.txtDepartmentName.Text = string.Empty;
        }
    }
}
